{% extends 'admin/common/base.html' %}
{% block body %}
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-menu-edit">
			<input id="treeId" name="parent_id" type="hidden" value="{{d.parent_id}}" />
			<input name="id"   type="hidden" value="{{d.id}}"   />
			<div class="form-group">
				<label class="col-sm-3 control-label">上级菜单：</label>
				<div class="col-sm-8">
				    <div class="input-group">
					    <input class="form-control" type="text" onclick="selectMenuTree()" id="treeName" readonly="true" value="{{'无' if d.parent == None else d.parent.name}}">
				        <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">菜单类型：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" {% if d.menu_type == 'M' %} checked="checked" {% endif %} name="menu_type" value="M" /> 目录 </label> 
					<label class="radio-box"> <input type="radio" {% if d.menu_type == 'C' %} checked="checked" {% endif %} name="menu_type" value="C" /> 菜单 </label> 
					<label class="radio-box"> <input type="radio" {% if d.menu_type == 'F' %} checked="checked" {% endif %} name="menu_type" value="F" /> 按钮 </label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">菜单名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="name" id="name" value="{{d.name}}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">请求地址：</label>
				<div class="col-sm-8">
					<input id="url" name="url" class="form-control" type="text" value="{{d.url}}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">打开方式：</label>
				<div class="col-sm-8">
					<select id="target" name="target" class="form-control m-b">
	                    <option value="menuItem" {% if d.target == 'menuItem' %} selected {% endif %}>页签</option>
	                    <option value="menuBlank" {% if d.target == 'menuBlank' %} selected {% endif %}>新窗口</option>
	                </select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">权限标识：</label>
				<div class="col-sm-8">
					<input id="perms" name="perms" class="form-control" type="text" value="{{d.perms}}">
					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限标识，如：@RequiresPermissions("")</span>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="order_num" value="{{d.order_num}}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">图标：</label>
				<div class="col-sm-8">
					<input id="icon" name="icon" class="form-control" type="text" placeholder="选择图标" value="{{d.icon}}">
                    <div class="ms-parent" style="width: 100%;">
                        <div class="icon-drop animated flipInX" style="display: none;max-height:200px;overflow-y:auto">
                            <div>
                                {% include 'admin/sys/menu/icon.html'%}
                            </div>
                        </div>
                    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单状态：</label>
				<div class="col-sm-3">
				    <div class="radio-box">
						<input type="radio" id="4" name="visible" value="0" {% if d.visible == 0 %} checked="checked" {% endif %}>
						<label for="4">显示</label>
					</div>
				    <div class="radio-box">
						<input type="radio" id="5" name="visible" value="1" {% if d.visible == 1 %} checked="checked" {% endif %}>
						<label for="5">隐藏</label>
					</div>
				</div>
				<label class="col-sm-2 control-label is-refresh" title="打开菜单选项卡是否刷新页面">是否刷新：<i class="fa fa-question-circle-o"></i></label>
				<div class="col-sm-3 is-refresh">
				    <div class="radio-box">
						<input type="radio" id="refresh-no" name="is_refresh" value="1" {% if d.is_refresh == 0 %} checked="checked" {% endif %}>
						<label for="refresh-no">否</label>
					</div>
					<div class="radio-box">
						<input type="radio" id="refresh-yes" name="is_refresh" value="0" {% if d.is_refresh == 1 %} checked="checked" {% endif %}>
						<label for="refresh-yes">是</label>
					</div>
				</div>
			</div>
		</form>
	</div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
	 <script>
        var prefix = "system/menu";
        
		$(function() {
            var menuType = $('input[name="menu_type"]:checked').val();
            menuVisible(menuType);
        });

        $("#form-menu-edit").validate({
        	onkeyup: false,
        	rules:{
        		menuType:{
        			required:true,
        		},
        		menuName:{
        			// remote: {
                    //     url: prefix + "/checkMenuNameUnique",
                    //     type: "post",
                    //     dataType: "json",
                    //     data: {
                    //     	"menuId": function() {
                    //             return $("#menuId").val();
                    //         },
                    //         "parentId": function() {
		            //     		return $("input[name='parentId']").val();
		            //         },
                	// 		"menuName": function() {
                    //             return $.common.trim($("#menuName").val());
                    //         }
                    //     },
                    //     dataFilter: function(data, type) {
                    //     	return $.validate.unique(data);
                    //     }
                    // }
        		},
        		orderNum:{
        			digits:true
        		},
        	},
        	messages: {
                "menuName": {
                    remote: "菜单已经存在"
                }
            },
            focusCleanup: true
        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	            $.operate.save("{{url_for('admin.sys.menu.edit')}}", $('#form-menu-edit').serialize());
	        }
	    }

        $(function() {
            $("input[name='icon']").focus(function() {
                $(".icon-drop").show();
            });
            $("#form-menu-edit").click(function(event) {
                var obj = event.srcElement || event.target;
                if (!$(obj).is("input[name='icon']")) {
                    $(".icon-drop").hide();
                }
            });
            $(".icon-drop").find(".ico-list i").on("click",
            function() {
                $('#icon').val($(this).attr('class'));
            });
            $('input').on('ifChecked',
            function(event) {
                var menuType = $(event.target).val();
                menuVisible(menuType);
            });
        });

        function menuVisible(menuType) {
            if (menuType == "M") {
                $("#url").parents(".form-group").hide();
                $("#perms").parents(".form-group").hide();
                $("#icon").parents(".form-group").show();
                $("#target").parents(".form-group").hide();
                $("input[name='visible']").parents(".form-group").show();
            } else if (menuType == "C") {
                $("#url").parents(".form-group").show();
                $("#perms").parents(".form-group").show();
                $("#icon").parents(".form-group").show();
                $("#target").parents(".form-group").show();
                $("input[name='visible']").parents(".form-group").show();
            } else if (menuType == "F") {
                $("#url").parents(".form-group").hide();
                $("#perms").parents(".form-group").show();
                $("#icon").parents(".form-group").hide();
                $("#target").parents(".form-group").hide();
                $("input[name='visible']").parents(".form-group").hide();
            }
        }

        /*菜单管理-修改-选择菜单树*/
        function selectMenuTree() {
        	var menuId = $("#treeId").val();
        	if(menuId > 0) {
        		var url = "{{url_for('admin.sys.menu.select_menu_tree')}}?id=" + menuId;
        		var options = {
       				title: '菜单选择',
       				width: "380",
       				url: url,
       				callBack: doSubmit
       			};
       			$.modal.openOptions(options);
        	} else {
        		$.modal.alertError("主菜单不能选择");
        	}
		}
		
		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
    </script>
{% endblock %}
